
//
// 120205 - AH - (Opera) Created.
//

package org.soekrat.engine.motor.passingstrategies;

import org.soekrat.engine.motor.ConversationElement;
import org.soekrat.engine.motor.Motor;
import org.soekrat.engine.wheel.Wheel;
import org.soekrat.engine.wheel.ontology.Ontology;
import org.soekrat.engine.wheel.retrieval.RetrievalEvent;

/**
 * <p>
 *   {@link PassingStrategy} that stop passing when something get retrieved.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
public final class CheckForContentPassingStrategy 
  extends SequenceBasedPassingStrategy 
{
  private int m_pos=-1;
  
  private int m_celevel=-1;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public CheckForContentPassingStrategy(){}
  
  @Override
  public Ontology getOntology(Motor motor, ConversationElement ce,
    RetrievalEvent re) 
  {
    Wheel wheel=motor.getWheel();
    
    if (m_pos==-1)
    {
      // Asked for the first Ontology, then just give the first.
      m_pos++;
      String name=m_seq.get(m_pos);
        
      Ontology retVal=wheel.getOntology(name);
     
      m_celevel=ce.getCounter();
      
      return retVal;
    }
    else
    {
      // Here we give only if nothing more has been retrieved.
      int level=ce.getCounter();
      
      if (level>m_celevel) return null;
      
      // If no more ontologies we give up; no turns here.
      if (m_pos==m_seq.size()-1) 
      {
        reset();
        return null;
      }
      
      m_pos++;
      String name=m_seq.get(m_pos);
      
      Ontology retVal=wheel.getOntology(name);
      
      return retVal;
    }
  }

  @Override
  public void reset()
  { 
    m_pos=-1;
    m_celevel=-1;
  }
  
}
